package com.yuqiang.manage.controller;

import com.yuqiang.common.model.ResultModel;
import com.yuqiang.common.util.DateGetUtil;
import com.yuqiang.manage.model.bo.*;
import com.yuqiang.manage.model.po.EinfoPo;
import com.yuqiang.manage.model.po.ExercisePlanPo;
import com.yuqiang.manage.model.po.ReserveRecordPo;
import com.yuqiang.manage.model.po.UsedRecordPo;
import com.yuqiang.manage.model.vo.*;
import com.yuqiang.manage.service.ManageService;
import com.yuqiang.user.model.bo.KeyBo;
import com.yuqiang.user.model.po.KeyPo;
import com.yuqiang.user.model.vo.KeyVo;
import com.yuqiang.user.service.KeyService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/5/5 0005.
 */
@Controller
@RequestMapping("/manage/")
public class ManageController {

    @Autowired
    private ManageService manageService;

    @Autowired
    private KeyService keyService;


    protected Map<String, Object> resultMap = new HashMap<String, Object>();

    /**
     * 器材信息管理程序入口
     * @return
     */
    @RequestMapping("/initEquipmentInfo")
    public String initEquipmentInfo(HttpServletRequest request) {
        if(null==request.getSession().getAttribute("userName")){
            return "/index/login";
        }
        return "/manage/EquipmentInfoManage";
    }

    /**
     *器材信息管理展示
     * @return
     */
    @RequestMapping("/queryEquipmentInfoList")
    @ResponseBody
    public Map<String, Object> queryEquipmentInfoList(
            @ModelAttribute EinfoBo einfoBo) {
        List<EinfoVo> listEquipmentInfoVo = manageService
                .getEquipmentInfoList(einfoBo);
        resultMap.put("rows", listEquipmentInfoVo);
        resultMap.put("total", manageService.getEquipmentInfoTotal(einfoBo));
        return resultMap;
    }

    /**
     *根据器材ID获取器材信息
     * @return
     */
    @RequestMapping("/queryEquipmentInfo")
    @ResponseBody
    public EinfoVo queryEquipmentInfo(
            @ModelAttribute EinfoBo einfoBo) {
        List<EinfoVo> listEquipmentInfoVo = manageService
                .getEquipmentInfoList(einfoBo);
        if(CollectionUtils.isNotEmpty(listEquipmentInfoVo)){
            return listEquipmentInfoVo.get(0);
        }
        return null;
    }

    /**
     *修改器材信息
     * @return
     */
    @RequestMapping("/saveEquipmentInfo")
    @ResponseBody
    public ResultModel saveEquipmentInfo(
            @ModelAttribute EinfoPo einfoPo,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
            resultModel.setSuccess(false);
            resultModel.setMessage("你不是管理员！");
            return resultModel;
        }
        einfoPo.setLastModifiedBy(request.getSession().getAttribute("userName").toString());
        manageService.updateEquipmentInfo(einfoPo);
        return resultModel;
    }

    /**
     *新增器材信息
     * @return
     */
    @RequestMapping("/insertEquipmentInfo")
    @ResponseBody
    public ResultModel insertEquipmentInfo(
            @ModelAttribute EinfoPo einfoPo,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
            resultModel.setSuccess(false);
            resultModel.setMessage("你不是管理员！");
            return resultModel;
        }
        einfoPo.setLastModifiedBy(request.getSession().getAttribute("userName").toString());
        manageService.insertEquipmentInfo(einfoPo);
        return resultModel;
    }


    /**
     *预定信息管理展示
     * @return
     */
    @RequestMapping("/getReserveRecordList")
    @ResponseBody
    public Map<String, Object> getReserveRecordList(
            @ModelAttribute ReserveRecordBo bo) {
        List<ReserveRecordVo> listReserveRecordVo = manageService
                .getReserveRecordList(bo);
        resultMap.put("rows", listReserveRecordVo);
        resultMap.put("total", manageService.getReserveRecordTotal(bo));
        return resultMap;
    }

    /**
     *预定器材
     * @return
     */
    @RequestMapping("/reserveEquipment")
    @ResponseBody
    public ResultModel reserveEquipment(
            @ModelAttribute ReserveRecordPo po,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        po.setuId(Integer.valueOf(request.getSession().getAttribute("userId").toString()));
        manageService.reserveEquipment(po);
        EinfoPo einfoPo = new EinfoPo();
        einfoPo.setUseState(222);
        einfoPo.seteId(po.geteId());
        einfoPo.setLastModifiedBy(request.getSession().getAttribute("userName").toString());
        manageService.updateEquipmentInfo(einfoPo);
        return resultModel;
    }

    /**
     *结束预定器材
     * @return
     */
    @RequestMapping("/stopReserveEquipment")
    @ResponseBody
    public ResultModel stopReserveEquipment(
            @ModelAttribute ReserveRecordPo po,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        manageService.stopReserveEquipment(po);
        EinfoPo einfoPo = new EinfoPo();
        einfoPo.setUseState(111);
        einfoPo.seteId(po.geteId());
        einfoPo.setLastModifiedBy(request.getSession().getAttribute("userName").toString());
        manageService.updateEquipmentInfo(einfoPo);
        return resultModel;
    }


    /**
     *使用信息管理展示
     * @return
     */
    @RequestMapping("/getUsedRecordList")
    @ResponseBody
    public Map<String, Object> getUsedRecordList(
            @ModelAttribute UsedRecordBo bo) {
        List<UsedRecordVo> listUsedRecordVo = manageService
                .getUsedRecordList(bo);
        resultMap.put("rows", listUsedRecordVo);
        resultMap.put("total", manageService.getUsedRecordTotal(bo));
        return resultMap;
    }

    /**
     *使用器材
     * @return
     */
    @RequestMapping("/useEquipment")
    @ResponseBody
    public ResultModel useEquipment(
            @ModelAttribute UsedRecordPo po,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        EinfoBo einfoBo = new EinfoBo();
        einfoBo.seteId(po.geteId());
        List<EinfoVo> listEquipmentInfoVo = manageService
                .getEquipmentInfoList(einfoBo);
        if(CollectionUtils.isNotEmpty(listEquipmentInfoVo)){
            po.setuId(Integer.valueOf(request.getSession().getAttribute("userId").toString()));
            if(listEquipmentInfoVo.get(0).getUseState()==111){
                manageService.useEquipment(po);
                //空闲转为使用中
                EinfoPo einfoPo = new EinfoPo();
                einfoPo.setUseState(333);
                einfoPo.seteId(po.geteId());
                einfoPo.setLastModifiedBy(request.getSession().getAttribute("userName").toString());
                manageService.updateEquipmentInfo(einfoPo);
            }else if(listEquipmentInfoVo.get(0).getUseState()==222){
                //预定中转为使用中，还要判断是否是当前用户预定的
                manageService.useEquipment(po);
                ReserveRecordPo reserveRecordPo = new ReserveRecordPo();
                reserveRecordPo.seteId(po.geteId());
                manageService.stopReserveEquipment(reserveRecordPo);

                EinfoPo einfoPo = new EinfoPo();
                einfoPo.setUseState(333);
                einfoPo.seteId(po.geteId());
                manageService.updateEquipmentInfo(einfoPo);
            }else{
                resultModel.setSuccess(false);
                resultModel.setMessage("器材状态异常！");
            }
        }else{
            resultModel.setSuccess(false);
            resultModel.setMessage("该器材不存在！");
        }
        return resultModel;
    }

    /**
     *结束使用器材
     * @return
     */
    @RequestMapping("/stopUseEquipment")
    @ResponseBody
    public ResultModel stopUseEquipment(
            @ModelAttribute UsedRecordPo po,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        manageService.stopUseEquipment(po);
        EinfoPo einfoPo = new EinfoPo();
        einfoPo.setUseState(111);
        einfoPo.seteId(po.geteId());
        einfoPo.setLastModifiedBy(request.getSession().getAttribute("userName").toString());
        manageService.updateEquipmentInfo(einfoPo);
        return resultModel;
    }


    /**
     *锻炼计划信息管理展示
     * @return
     */
    @RequestMapping("/getExercisePlanList")
    @ResponseBody
    public Map<String, Object> getExercisePlanList(
            @ModelAttribute ExercisePlanBo bo) {
        List<ExercisePlanVo> listExercisePlanVo = manageService
                .getExercisePlanList(bo);
        resultMap.put("rows", listExercisePlanVo);
        resultMap.put("total", manageService.getExercisePlanTotal(bo));
        return resultMap;
    }

    /**
     *生成明日计划
     * @return
     */
    @RequestMapping("/createExercisePlan")
    @ResponseBody
    public ResultModel createExercisePlan(
            @ModelAttribute ExercisePlanPo po,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
            resultModel.setSuccess(false);
            resultModel.setMessage("你不是管理员！");
            return resultModel;
        }
        po.setuId(po.getuId());
        po.setEpRank(7);
        resultModel = manageService.createExercisePlan(po);
        return resultModel;
    }


    /**
     *锻炼计划详细信息展示-无分页
     * @return
     */
    @RequestMapping("/getPlanDetailList")
    @ResponseBody
    public Map<String, Object> getPlanDetailList(
            @ModelAttribute PlanDetailBo bo) {
        List<PlanDetailVo> listPlanDetailVo = manageService
                .getPlanDetailList(bo);
        resultMap.put("rows", listPlanDetailVo);
        return resultMap;
    }

    /**
     *获取个人锻炼记录
     * @return
     */
    @RequestMapping("/getPersonalRecordList")
    @ResponseBody
    public Map<String, Object> getPersonalRecordList(
            @ModelAttribute ExercisePlanPo po,HttpServletRequest request) {
        if(null==request.getSession().getAttribute("userName")||"999".equals(request.getSession().getAttribute("userType"))){
            return null;
        }
        List<DoPlanVo> list = manageService.getPersonalRecordList(Integer.parseInt(request.getSession().getAttribute("userId").toString()));
        resultMap.put("rows", list);
        return resultMap;
    }

    /**
     *获取个人明日计划
     * @return
     */
    @RequestMapping("/getPersonalPlanDetailList")
    @ResponseBody
    public Map<String, Object> getPersonalPlanDetailList(
            @ModelAttribute ExercisePlanBo bo,HttpServletRequest request) {
        if(null==request.getSession().getAttribute("userName")||"999".equals(request.getSession().getAttribute("userType"))){
            return null;
        }
        bo.setuId(Integer.parseInt(request.getSession().getAttribute("userId").toString()));
        bo.setEpRank(7);
        List<PlanDetailVo> list  = manageService.getPersonalPlanDetailList(bo);
        resultMap.put("rows", list);
        return resultMap;
    }

    /**
     * 个人锻炼记录
     * @return
     */
    @RequestMapping("/PersonalRecord")
    public String PersonalRecord() {
        return "/manage/PersonalRecord";
    }

    /**
     * 个人明日计划
     * @return
     */
    @RequestMapping("/PersonalPlanDetail")
    public String PersonalPlanDetail() {
        return "/manage/PersonalPlanDetail";
    }


    /**
     * KEY信息管理程序入口
     * @return
     */
    @RequestMapping("/initKeyInfo")
    public String initKeyInfo(HttpServletRequest request) {
        if(null==request.getSession().getAttribute("userName")){
            return "/index/login";
        }
        if("999".equals(request.getSession().getAttribute("userType").toString())){

        }else{
            return "/index/personal";
        }
        return "/user/KeyManage";
    }

    /**
     *KEY信息管理展示
     * @return
     */
    @RequestMapping("/queryKeyInfoList")
    @ResponseBody
    public Map<String, Object> queryKeyInfoList(
            @ModelAttribute KeyBo keyBo, HttpServletRequest request) {
        List<KeyVo> listVo = keyService
                .selectKeyRows(keyBo);
        resultMap.put("rows", listVo);
        resultMap.put("total", keyService.selectKeyTotal(keyBo));
        return resultMap;
    }

    /**
     *修改KEY信息
     * @return
     */
    @RequestMapping("/saveKeyInfo")
    @ResponseBody
    public ResultModel saveKeyInfo(
            @ModelAttribute KeyPo keyPo, HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
            resultModel.setSuccess(false);
            resultModel.setMessage("你不是管理员！");
            return resultModel;
        }
        keyService.updateKeyById(keyPo);
        return resultModel;
    }

    /**
     *新增KEY信息
     * @return
     */
    @RequestMapping("/insertKeyInfo")
    @ResponseBody
    public ResultModel insertKeyInfo(
            @ModelAttribute KeyPo keyPo,HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        if(request.getSession().getAttribute("userName")==null){
            resultModel.setSuccess(false);
            resultModel.setMessage("请登录后再操作！");
            return resultModel;
        }
        if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
            resultModel.setSuccess(false);
            resultModel.setMessage("你不是管理员！");
            return resultModel;
        }
        String key = DateGetUtil.getKeyTime();
        keyPo.setCdKey(key.concat(request.getSession().getAttribute("userName").toString()));
        keyService.getKey(keyPo);
        return resultModel;
    }

    /**
     *获取单个器材使用记录
     * @return
     */
    @RequestMapping("/getEquipmentRecordList")
    @ResponseBody
    public Map<String, Object> getEquipmentRecordList(
            @ModelAttribute UsedRecordPo po) {
        List<DoPlanVo> list = manageService.getRecordByeId(po.geteId());
        resultMap.put("rows", list);
        return resultMap;
    }

    /**
     * 获取所有器材使用记录
     * @return
     */
    @RequestMapping("/initAllEquipmentRecord")
    public String initAllEquipmentRecord() {
        return "/manage/AllEquipmentInfo";
    }

    /**
     *获取所有器材使用记录
     * @return
     */
    @RequestMapping("/getAllEquipmentRecordList")
    @ResponseBody
    public Map<String, Object> getAllEquipmentRecordList(
            @ModelAttribute UsedRecordPo po) {
        List<DoPlanVo> list = manageService.getAllRecord();
        List<AllEquipmentVo> list2 = new ArrayList<AllEquipmentVo>();
        Integer i = 0;

        for(DoPlanVo vo : list){
            i = i+vo.getDoTime();
        }
        for(DoPlanVo vo : list){
            if(vo.geteType()==111){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("跑步机");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==222){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("划船器");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==333){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("肌肉组");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==444){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("仰卧器");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==555){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("健步车");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==666){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("美腰机");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==777){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("登梯器");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==888){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("引体杆");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else if(vo.geteType()==999){
                AllEquipmentVo allEquipmentVo = new AllEquipmentVo();
                allEquipmentVo.setName("瑜伽球");
                float y = (float)vo.getDoTime()/(float)i;
                allEquipmentVo.setY(y);
                list2.add(allEquipmentVo);
            }else{

            }
        }
        resultMap.put("rows", list2);
        return resultMap;
    }


    /**
     * 一周计划
     * @return
     */
    @RequestMapping("/initSevenDay")
    public String initSevenDay() {
        return "/manage/SevenDay";
    }

}
